package com.selfdriving.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.selfdriving.R;
import com.selfdriving.adapter.ActivityAmbitusQueryAdapter;
import com.selfdriving.bean.Location;
import com.selfdriving.bean.ScenicSpots;
import com.selfdriving.map.InitMap;
import com.selfdriving.map.ScenicLocation;
import com.selfdriving.utils.LogUtil;
import com.selfdriving.view.MoMoRefreshListView;
import com.selfdriving.view.MoMoRefreshListView.OnCancelListener;
import com.selfdriving.view.MoMoRefreshListView.OnRefreshListener;

/** 周边服务查询显示结果 主界面 **/
public class WActivityAmbitusQuery extends BaseActivity implements
		OnRefreshListener, OnCancelListener
{
	private TextView title;
	public static MoMoRefreshListView listView;
	public static BaseAdapter adapter;
	private String keyWord = null;
	public static InitMap initMap;
	private ImageView imgBreak;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.w_activity_ambitus_query);
		Intent intent = super.getIntent();
		keyWord = intent.getStringExtra("keyWord");
		initViews();
		initEvents();
		listView.mState = 2;
		listView.changeHeaderViewByState();
	}

	protected void initViews()
	{
		imgBreak = (ImageView) super.findViewById(R.id.head_break);
		imgBreak.setVisibility(View.VISIBLE);
		title = (TextView) super.findViewById(R.id.top_text_middle);
		title.setText(keyWord);
		listView = (MoMoRefreshListView) super
				.findViewById(R.id.ambitus_query_listview);
		adapter = new ActivityAmbitusQueryAdapter(mApplication,
				WActivityAmbitusQuery.this, mApplication.ScenicarrayList);
		listView.setAdapter(adapter);
		if (mApplication.ScenicarrayList.size() < 5)
		{
			listView.removeFooterView(listView.mFootView);
		}
		initMap = new InitMap(WActivityAmbitusQuery.this, handler, keyWord);
	}

	Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
			case 1:
				if (adapter != null)
				{
					if (mApplication.ScenicarrayList.size() < 5)
					{
						listView.removeFooterView(listView.mFootView);
					} else if (listView.getFooterViewsCount() < 1)
					{
						listView.addFooterView(listView.mFootView);
					}
					adapter.notifyDataSetChanged();
					listView.onRefreshComplete();

				}
				break;
			case -1:
				LogUtil.showCustomToast(WActivityAmbitusQuery.this, "没有更多了哟,亲！");
				listView.onRefreshComplete();
				break;
			}
			listView.onLoadMoreComplete(false);
		}
	};

	protected void initEvents()
	{
		listView.setOnRefreshListener(this);
		listView.setOnCancelListener(this);
		imgBreak.setOnClickListener(clickListener);
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3)
			{
				ScenicSpots scenic = (ScenicSpots) mApplication.ScenicarrayList
						.get(position);
				Bundle bundle = scenic.getScenicLocation();
				Location scenicLocation = bundle
						.getParcelable("scenicLocation");
				double Latitude = scenicLocation.getLatitude();
				double Longitude = scenicLocation.getLongitude();
				mApplication.geoPoint = new GeoPoint((int) Latitude,
						(int) Longitude);
				Intent intent = new Intent(WActivityAmbitusQuery.this,
						ScenicLocation.class);
				startActivity(intent);
			}
		});
	}

	OnClickListener clickListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			mApplication.ScenicarrayList.clear();
			mApplication.index = 1;
			finish();
		}
	};

	@Override
	public void onBackPressed()
	{
		mApplication.ScenicarrayList.clear();
		mApplication.index = 1;
		finish();
	}

	@Override
	public void onCancel()
	{
		// TODO Auto-generated method stub
		listView.onRefreshComplete();
	}

	@Override
	public void onRefresh()
	{
		mApplication.index = 1;
		mApplication.ScenicarrayList.clear();
		putAsyncTask(new AsyncTask<Void, Void, Boolean>()
		{
			@Override
			protected Boolean doInBackground(Void... params)
			{
				initMap.searchMost();
				return null;
			}

			@Override
			protected void onPostExecute(Boolean result)
			{
				super.onPostExecute(result);
				listView.onRefreshComplete();
			}
		});
	}

	@Override
	public void onLoadMore()
	{
		// TODO Auto-generated method stub
		initMap.searchMost();
	}
}
